﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;
using ListItem = ExtAspNet.ListItem;

namespace MyDMS_ExtJS.Admin
{
    public partial class ProjectEdit : PageBase
    {
        private int _Project_Code = 0;
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                _Project_Code = Convert.ToInt32(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                BindDepartment();
                BindUser();
                var sf = _myDmsEntity.Project.FirstOrDefault(s => s.Project_Code == _Project_Code);
                if (sf != null)
                {
                    tbName.Text = sf.Project_Title;
                    tbDesc.Text = sf.Project_Desc;
                    ddlDepartment.SelectedValue = sf.Department_Code.ToString();
                    ddlUser.SelectedValue = sf.Assigned_To.ToString();
                    cbIsAnonymous.Checked = sf.Is_Anonymous;
                }
            }
        }

        private void BindDepartment()
        {
            var department = _myDmsEntity.Department.ToList();
            ddlDepartment.DataSource = department;
            ddlDepartment.DataTextField = "Department_Name";
            ddlDepartment.DataValueField = "Department_Code";
            ddlDepartment.DataBind();
            ddlDepartment.Items.Insert(0, new ListItem("选择科室", "-1"));
        }
        private void BindUser(int departmentCode=0)
        {
            if (departmentCode!=0)
            {
                
            ddlUser.DataSource  = from uu in _myDmsEntity.User.Where(u => u.Department_Code == departmentCode)
                       select new
                                  {
                                      UserName = uu.FirstName + uu.LastName,
                                      UserCode = uu.User_Code
                                  };
            }
            else
            {
                ddlUser.DataSource = from uu in _myDmsEntity.User
                                     select new
                                     {
                                         UserName = uu.FirstName + uu.LastName,
                                         UserCode = uu.User_Code
                                     };
            }
            ddlUser.DataTextField = "UserName";
            ddlUser.DataValueField = "UserCode";
            ddlUser.DataBind();
            ddlUser.Items.Insert(0, new ListItem("选择负责人", "-1"));
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (_Project_Code != 0)
            {
                var sf = _myDmsEntity.Project.FirstOrDefault(s => s.Project_Code== _Project_Code);
                if (sf != null)
                {
                    sf.Project_Title= tbName.Text;
                    sf.Project_Desc= tbDesc.Text;
                    sf.Is_Anonymous = cbIsAnonymous.Checked;
                    sf.Updation_Date = DateTime.Now;
                    sf.Assigned_To = Convert.ToInt32(ddlUser.SelectedValue);
                    sf.Department_Code = Convert.ToInt32(ddlDepartment.SelectedValue);
                }
                _myDmsEntity.SaveChanges();
            }
            else
            {
                _myDmsEntity.Project.AddObject(new MyDMS.DAL.Project()
                {
                    Project_Title= tbName.Text,
                    Project_Desc= tbDesc.Text,
                    Is_Anonymous = cbIsAnonymous.Checked,
                    Updation_Date = DateTime.Now,
                    Assigned_To = Convert.ToInt32(ddlUser.SelectedValue),
                    Department_Code = Convert.ToInt32(ddlDepartment.SelectedValue),
                    Creation_Date = DateTime.Now,
                    Creation_By = Author.office_Code
                });
                _myDmsEntity.SaveChanges();
            }

            // 2. 关闭本窗体，然后刷新父窗体
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindUser(Convert.ToInt32(ddlDepartment.SelectedValue));
        }
    }
}